home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / password / source / removepassword.c < prev   
C/C++ Source or Header  |  1993-04-26  |  11KB  |  375 lines

  1. #include <exec/memory.h>
  2. #include <exec/execbase.h>
  3. #include <dos/filehandler.h>
  4. #include <intuition/intuitionbase.h>
  5. #include <devices/hardblocks.h>
  6. #include <devices/input.h>
  7. #include <devices/conunit.h>
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <proto/intuition.h>
  12.  
  13. #include <MyStartup.h>
  14. #include <MyLib.h>
  15.  
  16. /***********************************************/
  17.  
  18. LONG DoPkt1(struct MsgPort *, LONG, LONG);
  19. #pragma libcall DOSBase DoPkt1 F0 32103
  20.  
  21. /***********************************************/
  22.  
  23. #define BLOCK_SIZE 512
  24.  
  25. #define NoBlock 0xffffffff
  26.  
  27. #define BLOCKS_NEEDED 4
  28.  
  29. #define PASSWORD_SIZE 40
  30.  
  31. /***********************************************/
  32.  
  33. char __chip Hardblock[BLOCK_SIZE];
  34.  
  35. struct RDArgs *RDArgs;
  36.  
  37. struct IntuitionBase *IntuitionBase;
  38.  
  39. struct IOStdReq IORequest;
  40. struct IOStdReq InputRequest;
  41.  
  42. struct Window *MyWindow;
  43. struct Task *MyTask;
  44.  
  45. struct
  46.    {
  47.       char *Drive;
  48.    } Arguments;
  49.  
  50. char VersionString[]="$VER: Password 1.1 (26.04.93)";
  51.  
  52. ULONG PasswordBlock;
  53. int PasswordIndex;
  54.  
  55. char PasswordBuffer[PASSWORD_SIZE];
  56. int BufferIndex;
  57. BOOL Locked;
  58.  
  59. ULONG RDBBlock;
  60.  
  61. /***********************************************/
  62.  
  63. void CloseAll(int RC)
  64.  
  65. {
  66.    if (InputRequest.io_Command==IND_ADDHANDLER)
  67.       {
  68.          InputRequest.io_Command=IND_REMHANDLER;
  69.          DoIO(&InputRequest);
  70.          CloseDevice(&InputRequest);
  71.       }
  72.    CloseDevice(&IORequest);
  73.    CloseLibrary(IntuitionBase);
  74.    FreeArgs(RDArgs);
  75.    exit(RC);
  76. }
  77.  
  78. /***********************************************/
  79.  
  80. void LoadBlock(int BufferSize, ULONG BlockNumber, ULONG BlockID)
  81.  
  82. {
  83.    int i;
  84.    ULONG SummedLongs;
  85.    LONG CheckSum;
  86.    ULONG *Temp;
  87.  
  88.    if (CheckSignal(SIGBREAKF_CTRL_C))
  89.       {
  90.          PrintFault(ERROR_BREAK,"RemovePassword");
  91.          CloseAll(RETURN_WARN);
  92.       }
  93.    IORequest.io_Command=CMD_READ;
  94.    IORequest.io_Length=BLOCK_SIZE;
  95.    IORequest.io_Data=Hardblock;
  96.    IORequest.io_Offset=BlockNumber*BLOCK_SIZE;
  97.    if (DoIO(&IORequest))
  98.       {
  99.          printf("Error reading block %ld\n",BlockNumber);
  100.          CloseAll(RETURN_FAIL);
  101.       }
  102.    if (BlockID)
  103.       {
  104.          if (BlockID!=((struct RigidDiskBlock *)Hardblock)->rdb_ID)
  105.             {
  106.                printf("Block %ld: exspected type 0x%lx got type 0x%lx\n",BlockNumber,BlockID,((struct RigidDiskBlock *)Hardblock)->rdb_ID);
  107.                CloseAll(RETURN_FAIL);
  108.             }
  109.          SummedLongs=((struct RigidDiskBlock *)Hardblock)->rdb_SummedLongs;
  110.          CheckSum=0;
  111.          Temp=(ULONG *)Hardblock;
  112.          for (i=0; i<SummedLongs; i++)
  113.             {
  114.                CheckSum=CheckSum+(*Temp++);
  115.             }
  116.          if (CheckSum)
  117.             {
  118.                printf("Checksum of block %ld is invalid.\n",BlockNumber);
  119.                CloseAll(RETURN_FAIL);
  120.             }
  121.       }
  122. }
  123.  
  124. /***********************************************/
  125.  
  126. void SearchPassword(void)
  127.  
  128. {
  129.    BOOL FoundRDB;
  130.  
  131.    FoundRDB=FALSE;
  132.    for (RDBBlock=0; RDBBlock<RDB_LOCATION_LIMIT && !FoundRDB; )
  133.       {
  134.          LoadBlock(sizeof(struct RigidDiskBlock),RDBBlock,0);
  135.          if (*(ULONG *)Hardblock==IDNAME_RIGIDDISK)
  136.             {
  137.                FoundRDB=TRUE;
  138.             }
  139.          else
  140.             {
  141.                RDBBlock++;
  142.             }
  143.       }
  144.    if (!FoundRDB)
  145.       {
  146.          printf("Sorry, I couldn't locate the RigidDiskBlock on this unit!\n");
  147.          CloseAll(RETURN_FAIL);
  148.       }
  149.    printf("Found RigidDiskBlock on block %ld\n",RDBBlock);
  150.    LoadBlock(sizeof(struct RigidDiskBlock),RDBBlock,IDNAME_RIGIDDISK);
  151.    PasswordBlock=((struct RigidDiskBlock *)Hardblock)->rdb_DriveInit;
  152.    while (PasswordBlock!=NoBlock)
  153.       {
  154.          LoadBlock(sizeof(struct LoadSegBlock),PasswordBlock,IDNAME_LOADSEG);
  155.          for (PasswordIndex=20; PasswordIndex<BLOCK_SIZE; PasswordIndex++)
  156.             {
  157.                if (!__builtin_strcmp(Hardblock+PasswordIndex,VersionString))
  158.                   {
  159.                      PasswordIndex+=__builtin_strlen(VersionString)+1;
  160.                      return;
  161.                   }
  162.             }
  163.          PasswordBlock=((struct LoadSegBlock *)Hardblock)->lsb_Next;
  164.       }
  165.    printf("Sorry, there is no password on this unit!\n");
  166.    CloseAll(RETURN_WARN);
  167. }
  168.  
  169. /***********************************************/
  170.  
  171. void OpenDriveDevice(void)
  172.  
  173. {
  174.    struct DosList *DosList;
  175.    struct FileSysStartupMsg *StartupMsg;
  176.    char *Device;
  177.    ULONG Unit;
  178.    int RC;
  179.  
  180.    RC=0;
  181.    DosList=LockDosList(LDF_READ | LDF_DEVICES);
  182.    if (DosList=FindDosEntry(DosList,Arguments.Drive,LDF_DEVICES))
  183.       {
  184.          StartupMsg=(struct FileSysStartupMsg *)BADDR(DosList->dol_misc.dol_handler.dol_Startup);
  185.          IORequest.io_Message.mn_ReplyPort=&((struct Process *)SysBase->ThisTask)->pr_MsgPort;
  186.          Device=((char *)BADDR(StartupMsg->fssm_Device))+1;
  187.          Unit=StartupMsg->fssm_Unit;
  188.          if (OpenDevice(Device,Unit,&IORequest,0))
  189.             {
  190.                printf("Unable to open %s unit %lu\n",Device,Unit);
  191.                RC=RETURN_FAIL;
  192.             }
  193.          else
  194.             {
  195.                printf("Locating password on %s unit %ld\n",Device,Unit);
  196.             }
  197.       }
  198.    else
  199.       {
  200.          printf("\x22%s:\x22 not found.\n",Arguments.Drive);
  201.          RC=RETURN_FAIL;
  202.       }
  203.    UnLockDosList(LDF_READ | LDF_DEVICES);
  204.    if (RC) CloseAll(RC);
  205. }
  206.  
  207. /***********************************************/
  208.  
  209. struct InputEvent * __asm __saveds InputHandler(register __a0 struct InputEvent *InputEvent)
  210.  
  211. {
  212.    struct InputEvent *InputEvents;
  213.    ULONG IntuiLock;
  214.    struct Window *ActiveWindow;
  215.  
  216.    InputEvents=InputEvent;
  217.    while (InputEvent)
  218.       {
  219.          IntuiLock=LockIBase(0);
  220.          ActiveWindow=IntuitionBase->ActiveWindow;
  221.          UnlockIBase(IntuiLock);
  222.          if (ActiveWindow==MyWindow)
  223.             {
  224.                if (InputEvent->ie_Class==IECLASS_RAWKEY)
  225.                   {
  226.                      if (!(InputEvent->ie_Code&IECODE_UP_PREFIX))
  227.                         {
  228.                            if (InputEvent->ie_Code<0x60 && !Locked)
  229.                               {
  230.                                  switch(InputEvent->ie_Code)
  231.                                     {
  232.                                        case 0x44:
  233.                                        case 0x50: PasswordBuffer[BufferIndex]=0xfe;
  234.                                                   Locked=TRUE;
  235.                                                   Signal(MyTask,SIGBREAKF_CTRL_E);
  236.                                                   break;
  237.                                        case 0x41: if (BufferIndex)
  238.                                                      {
  239.                                                         BufferIndex--;
  240.                                                      }
  241.                                                   break;
  242.                                        default:   if (BufferIndex<PASSWORD_SIZE-2)
  243.                                                      {
  244.                                                         if (InputEvent->ie_Qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  245.                                                            {
  246.                                                               InputEvent->ie_Code|=0x80;
  247.                                                            }
  248.                                                         PasswordBuffer[BufferIndex++]=InputEvent->ie_Code;
  249.                                                      }
  250.                                                   break;
  251.                                     }
  252.                               }
  253.                         }
  254.                      InputEvent->ie_Class=IECLASS_NULL;
  255.                   }
  256.             }
  257.          InputEvent=InputEvent->ie_NextEvent;
  258.       }
  259.    return(InputEvents);
  260. }
  261.  
  262. /***********************************************/
  263.  
  264. void InstallInputHandler(void)
  265.  
  266. {
  267.    static struct Interrupt Interrupt=
  268.       {
  269.          NULL,NULL,
  270.          NT_INTERRUPT,
  271.          127,
  272.          "RemovePassword",
  273.          NULL,
  274.          (void (*)())InputHandler
  275.       };
  276.  
  277.    struct InfoData __aligned InfoData;
  278.  
  279.    MyTask=SysBase->ThisTask;
  280.    Locked=TRUE;
  281.    InputRequest.io_Message.mn_ReplyPort=IORequest.io_Message.mn_ReplyPort;
  282.    DoPkt1(((struct FileHandle *)BADDR(Input()))->fh_Type,ACTION_DISK_INFO,MKBADDR(&InfoData));
  283.    MyWindow=((struct ConUnit *)(((struct IOStdReq *)(InfoData.id_InUse))->io_Unit))->cu_Window;
  284.    if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0)) ||
  285.        OpenDevice("input.device",0,&InputRequest,0))
  286.       {
  287.          CloseAll(RETURN_FAIL);
  288.       }
  289.    InputRequest.io_Command=IND_ADDHANDLER;
  290.    InputRequest.io_Data=&Interrupt;
  291.    DoIO(&InputRequest);
  292. }
  293.  
  294. /***********************************************/
  295.  
  296. void EnterOldPassword(void)
  297.  
  298. {
  299.    int Try;
  300.  
  301.    for (Try=0; Try<3; Try++)
  302.       {
  303.          printf("Please enter the current password: ");
  304.          Flush(Output());
  305.          BufferIndex=0;
  306.          Locked=FALSE;
  307.          Wait(SIGBREAKF_CTRL_E);
  308.          printf("\n");
  309.          while (BufferIndex>=0)
  310.             {
  311.                if (PasswordBuffer[BufferIndex]!=Hardblock[PasswordIndex+BufferIndex])
  312.                   {
  313.                      printf("Wrong password!\n");
  314.                      break;
  315.                   }
  316.                BufferIndex--;
  317.             }
  318.          if (BufferIndex<0)
  319.             {
  320.                return;
  321.             }
  322.       }
  323.    CloseAll(RETURN_ERROR);
  324. }
  325.  
  326. /***********************************************/
  327.  
  328. void RemovePassword(void)
  329.  
  330. {
  331.    int i;
  332.    long CheckSum;
  333.  
  334.    LoadBlock(sizeof(struct RigidDiskBlock),RDBBlock,IDNAME_RIGIDDISK);
  335.    ((struct RigidDiskBlock *)Hardblock)->rdb_DriveInit=NoBlock;
  336.    ((struct RigidDiskBlock *)Hardblock)->rdb_ChkSum=0;
  337.    CheckSum=0;
  338.    for (i=0; i<((struct RigidDiskBlock *)Hardblock)->rdb_SummedLongs; i++)
  339.       {
  340.          CheckSum+=((long *)Hardblock)[i];
  341.       }
  342.    ((struct RigidDiskBlock *)Hardblock)->rdb_ChkSum=0-CheckSum;
  343.    printf("Writing block %ld\n",RDBBlock);
  344.    IORequest.io_Command=CMD_WRITE;
  345.    IORequest.io_Length=BLOCK_SIZE;
  346.    IORequest.io_Data=Hardblock;
  347.    IORequest.io_Offset=RDBBlock*BLOCK_SIZE;
  348.    if (DoIO(&IORequest))
  349.       {
  350.          printf("Error writing block %ld\n",RDBBlock);
  351.          CloseAll(RETURN_FAIL);
  352.       }
  353. }
  354.  
  355. /***********************************************/
  356.  
  357. void main(void)
  358.  
  359. {
  360.    if (CommandLineLength)
  361.       {
  362.          if (RDArgs=ReadArgs("DRIVE/A",(long *)&Arguments,NULL))
  363.             {
  364.                OpenDriveDevice();
  365.                SearchPassword();
  366.                InstallInputHandler();
  367.                EnterOldPassword();
  368.                RemovePassword();
  369.                CloseAll(RETURN_OK);
  370.             }
  371.          PrintFault(IoErr(),"RemovePassword");
  372.       }
  373.    CloseAll(RETURN_FAIL);
  374. }
  375.